
from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app
from google.appengine.ext.webapp import template
import os
import cgi




class EventViewBuilder:
    
    
        def print_header(self):
            header = """
            <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"

             "http://www.w3.org/TR/html4/loose.dtd"> 
        
            <html>

            <link rel="stylesheet" type="text/css" href="stylesheets/eventinfo.css" />
            <head>

            </head>"""
            return header
        
        def print_footer(self):
            
            footer = """</body>

            </html>"""
            return footer
        
       
        
        def print_header_details(self,name,location,link):
            result = ""
            result+= "<p> Event Name: " +name+ "</p>"
            maps = "http://maps.google.com/maps?q="
            maps += location
            gmaps_link = "<a href =" +'"' +maps +'"' + "target=" +'"' +"_blank" +'"' + '>' +name +" @ " +location +"</a>"
            
            result+= "<p> Event Location: " +gmaps_link+ "</p>"
            
           
            result+= "<p> Event Link: " +link + "</p>"
            return result;
        
        def print_body(self,event):
            body = ""
            body_title = "<body>"

            body+=body_title
            body+=""" <center> <div id= "stylized" class = Event_Container>"""
            body+= self.print_club_icon(event['profile_link'])
            body+= self.print_header_details(event['name'], event['location'], event['link'])
            body+="<div class = divider></div>"
            body+= self.print_event_details(event['date'], event['start_time'], event['location'])
            body+="<div class = divider></div>"
            body+="""<div class = "container">"""
            body+= self.print_icons(event['age'], event['price'], event['genre'])
            body+= self.print_icons(event['VIP'], event['promoted'], event['day'])
            
            body+="""</div>"""
            body+="""</center>"""
            body += "</body>"
            return body
        
        def print_event_details(self, date, start_time, location):
            result = """<div class ="Event_Details">"""
            result+= "Date: " +date +"<br>" 
            result+= "Start Time: " +start_time +"<br>" 
            result+= "Location: " +location 
            result+="""</div>"""
            return result
        
        def print_club_icon(self,url):
            
            result ="""<img class = "profile" src= """ +'"' +url +'"' +" alt>"
            return result;
        
            
            
        def print_icons(self, para1, para2,para3):
            icon_container ="""<table id = "daytable_top">"""
            icon_container += "<tr>"
            
            icon_container += """<td><img class="zoom"  src=""" +'"' +"images/" +para1 +".png"+'"/></td>'
            icon_container += """<td><img class="zoom"  src=""" +'"' +"images/" +para2 +".png"+'"/></td>'
            icon_container += """<td><img class="zoom"  src=""" +'"' +"images/" +para3 +".png"+'"/></td>'
            icon_container += "</tr>"    
            icon_container+="""</table>"""
            return icon_container
        
        
        '''
        Instead of day as an argument, why not use  key-value pair
        and make this function generic for days, genres etc etc?
        '''
        def generate(self,event):
            
            finalstring=""
            finalstring += self.print_header()
            finalstring += self.print_body(event)
            finalstring += self.print_footer()
            return finalstring;
        
        def create_club_icon(self, event, icon):
            url = event["eventlink"]
            profile_pic_link = event["profilepic"]
            icon = "<a href='" + url + "'>" 
            
            icon += """<img class = "zoom" src=""" + profile_pic_link
            icon +=""" alt="">"""
            icon += "</a>"
                
            return icon
        
        
        def print_location(self,url):
            url = self.put_into_gmaps_url(url)
            icon = "<a href='" + url + "'>" 
            icon += """</a>"""
                
            return icon
                
        def put_into_gmaps_url(self,addr):
            encoded = ""
            
            for c in addr:
                
                if c == ' ':
                    c = '+'
                    
                encoded += c
            url = "https://maps.google.ie/maps?q=" + encoded
            return url


class EventDetailsPage(webapp.RequestHandler):
    
    
    '''
     Post function for generating page for a specific day
    '''
    def get(self):
        
        event = {}
        event['name'] = "Test Event"
        event['link'] = """http://www.tcd.ie/"""
        event['location'] = "Trinity College Dublin"
        event['profile_link'] = "http://www.american.edu/uploads/profiles/large/chris_palmer_profile_11.jpg"
        event['start_time'] = "9"
        event['age'] = "Age18"
        event['price'] = "EntryFive"
        event['genre'] = "Rock"
        event['promoted'] = "Promoted"
        event['day'] = "Monday"
        event['VIP'] = "VIP"
        event['date'] = """12/12/12"""
        
        builder = EventViewBuilder()
        htmlpage = builder.generate(event)
        self.response.out.write(htmlpage)
        
        
        #path = os.path.join(os.path.dirname(__file__), 'clubview.html')
        #self.response.out.write(template.render(path,''))
    
    def post(self):
        
       
        #post_data = self.request.get('day') #gets the day
      
        events =[]
        event ={}

        event['name'] = "Test Event"
        event['link'] = """http://www.tcd.ie/"""
        event['location'] = "Trinity College Dublin"
        event['profile_link'] = """http://www.american.edu/uploads/profiles/large/chris_palmer_profile_11.jpg"""
        event['start_time'] = "9"
        event['age'] = "Age18"
        event['price'] = "EntryFive"
        event['genre'] = "Rock"
        event['promoted'] = "Promoted"
        event['day'] = "Monday"
        event['VIP'] = "VIP"
        event['date'] = """12/12/12"""
        
        #eventlist = DBQuery()
    #this shit is wrong, we need search to be <category,value> e.g. searching by date <"date","19/12/2012">
        #events = eventlist.query(search)
        #events = []
        #events.append(event1)
        
       # builder = EventViewBuilder()
       # htmlpage = builder.generate(event1)
        #self.response.out.write(htmlpage)
        
        path = os.path.join(os.path.dirname(__file__), 'clubview.html')
        self.response.out.write(template.render(path,''))
        
            

application = webapp.WSGIApplication([('/EventDetails', EventDetailsPage)], debug=True)

def main():
    run_wsgi_app(application)

if __name__ == "__main__":
    main()
